package com.itheima.service.store.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CourseDao;
import com.itheima.domain.store.Course;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.store.CourseService;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CourseServiceImpl implements CourseService {

    @Override
    public PageInfo<Course> findAll(String pageStr, String sizeStr) {
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(sizeStr)) {
            size = Integer.parseInt(sizeStr);
        }
        PageHelper.startPage(page, size);
        CourseDao dao = DaoInstanceFactory.getMapper(CourseDao.class);
        List<Course> list = dao.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public void save(Course course) {
        CourseDao dao = DaoInstanceFactory.getMapper(CourseDao.class);
        String ID = UUID.randomUUID().toString();
        //添加ID
        course.setId(ID);
        //创建添加时间
        course.setCreateTime(new Date());
        dao.save(course);
    }

    @Override
    public Course findById(String id) {
        CourseDao dao = DaoInstanceFactory.getMapper(CourseDao.class);
        Course course = dao.findById(id);
        return course;
    }

    @Override
    public void update(Course course) {
        CourseDao dao = DaoInstanceFactory.getMapper(CourseDao.class);
        dao.update(course);
    }

    @Override
    public void delete(String id) {
        CourseDao dao = DaoInstanceFactory.getMapper(CourseDao.class);
        dao.delete(id);
    }

    @Override
    public List<Course> findAll() {
        CourseDao dao = DaoInstanceFactory.getMapper(CourseDao.class);
        return dao.findAll();
    }


}
